package br.com.covosys.util;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class JSFUtils {
	
	public static FacesContext getContext() {
		return FacesContext.getCurrentInstance();
	}
	
	public static HttpSession getSession(boolean createNew) {
		HttpSession httpSession = (HttpSession) getContext().getExternalContext().getSession(createNew);
		return httpSession;
	}
	
	public static HttpSession getSession() {
		return getSession(true);
	}
	
	public static void addMessage(String mensagem) {
		getContext().addMessage(null, new FacesMessage(mensagem));
	}
	
	public static HttpServletRequest getHttpServletRequest() {
		return (HttpServletRequest) getContext().getExternalContext().getRequest();
	}
	
	public static HttpServletResponse getHttpServletResponse() {
		return (HttpServletResponse) getContext().getExternalContext().getResponse();
	}	
	
	public static void redirect(String url) {
		try {
			getContext().getExternalContext().redirect(url);
		} catch (Exception exception) {
			throw new RuntimeException(exception);
		}
	}
}
